package org.xxpay.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xxpay.core.entity.PayDivisionRecord;
import org.xxpay.core.entity.PayDivisionRecordExample;
import org.xxpay.core.service.IPayDivisionRecordService;
import org.xxpay.service.dao.mapper.PayDivisionRecordMapper;

import java.util.List;

@Service
public class PayDivisionRecordServiceImpl implements IPayDivisionRecordService {

    @Autowired
    private PayDivisionRecordMapper recordMapper;


    @Override
    public List<PayDivisionRecord> select(int offset, int limit, PayDivisionRecord condition) {
        PayDivisionRecordExample example = new PayDivisionRecordExample();
        example.setOrderByClause("CreateTime desc");
        example.setOffset(offset);
        example.setLimit(limit);
        PayDivisionRecordExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, condition);
        return recordMapper.selectByExample(example);
    }

    @Override
    public Integer count(PayDivisionRecord condition) {
        PayDivisionRecordExample example = new PayDivisionRecordExample();
        PayDivisionRecordExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, condition);
        return recordMapper.countByExample(example);
    }

    @Override
    public PayDivisionRecord findById(Long id) {
        return recordMapper.selectByPrimaryKey(id);
    }

    @Override
    public int add(PayDivisionRecord PayDivisionRecord) {

        return recordMapper.insertSelective(PayDivisionRecord);
    }

    @Override
    public int update(PayDivisionRecord PayDivisionRecord) {
        return recordMapper.updateByPrimaryKeySelective(PayDivisionRecord);
    }

    @Override
    public int updateByOrderId(String payOrderId, PayDivisionRecord updateRecord) {

        PayDivisionRecordExample exa = new PayDivisionRecordExample();
        exa.createCriteria().andPayOrderIdEqualTo(payOrderId);
        return recordMapper.updateByExampleSelective(updateRecord, exa);
    }

    @Override
    public List<PayDivisionRecord> selectByPayOrderId(String payOrderId) {
        PayDivisionRecordExample exa = new PayDivisionRecordExample();
        exa.createCriteria().andPayOrderIdEqualTo(payOrderId);
        return recordMapper.selectByExample(exa);
    }

    @Override
    public List<PayDivisionRecord> selectByAgent(int offset, int limit, Long currentAgentId, PayDivisionRecord condition) {

        return recordMapper.selectByAgent(offset, limit, currentAgentId, condition);
    }

    @Override
    public int countByAgent(Long currentAgentId, PayDivisionRecord condition) {

        return recordMapper.countByAgent(currentAgentId, condition);
    }




    void setCriteria(PayDivisionRecordExample.Criteria criteria, PayDivisionRecord obj) {
        if(obj != null) {
            if(obj.getId() != null ) criteria.andIdEqualTo(obj.getId());
            if(StringUtils.isNotEmpty(obj.getIfTypeCode())) criteria.andIfTypeCodeEqualTo(obj.getIfTypeCode());
            if(obj.getInfoId() != null) criteria.andInfoIdEqualTo(obj.getInfoId());
            if(obj.getInfoType() != null) criteria.andInfoTypeEqualTo(obj.getInfoType());
            if(StringUtils.isNotEmpty(obj.getChannelMchId())) criteria.andChannelMchIdEqualTo(obj.getChannelMchId());
            if(obj.getStatus() != null) criteria.andStatusEqualTo(obj.getStatus());
            if(StringUtils.isNotEmpty(obj.getPayOrderId())) criteria.andPayOrderIdEqualTo(obj.getPayOrderId());
        }
    }

}
